import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/widget/group_text_filed.dart';
import 'package:image_pickers/image_pickers.dart';

import '../../../hj_utils/api_service/mine_service.dart';
import '../../../hj_utils/global.dart';
import '../../mine_business/collec_history_buy/col_his_buy_sub_page.dart';

class CommentFootView extends StatefulWidget {
  final VideoModel? videoModel;
  final Function(bool) begainCommentHandler; // true 发送
  final bool isSendStatus;
  final TextEditingController textEditController;
  final String? imagePath;
  final Function(String?)? imgCallback;
  final bool isShowCollect; //true: 显示收藏
  const CommentFootView({
    super.key,
    this.videoModel,
    required this.begainCommentHandler,
    this.isSendStatus = false,
    required this.textEditController,
    this.imagePath,
    this.imgCallback,
    this.isShowCollect = false,
  });

  @override
  State<CommentFootView> createState() => _CommentFootViewState();
}

class _CommentFootViewState extends State<CommentFootView> {
  bool isImageSelectIng = false;

  void choseImg() async {
    isImageSelectIng = true;
    final listMedia = await ImagePickers.pickerPaths(
      uiConfig: UIConfig(uiThemeColor: Colors.white),
      galleryMode: GalleryMode.image,
      selectCount: 1,
      showCamera: false,
    );
    if (listMedia.isNotEmpty) {
      final ret = listMedia.map((e) => e.path ?? '').toList();
      String selectedImgPath = ret.first;
      widget.imgCallback?.call(selectedImgPath);
    }
    setState(() {});
    Future.delayed(Duration(milliseconds: 500), () {
      isImageSelectIng = false;
    });
  }

  bool _isCollectLoading = false;

  bool get isCollect => widget.videoModel?.vidStatus?.hasCollected ?? false;

  void _onCollectEvent() async {
    try {
      bool preCollect = isCollect;
      if (_isCollectLoading) return;
      _isCollectLoading = true;

      await MineService.postCollect(widget.videoModel?.id, LoadDataType.post.collectType, !isCollect);

      if (!preCollect) {
        widget.videoModel?.collectCount = (widget.videoModel?.collectCount ?? 0) + 1;
      } else {
        widget.videoModel?.collectCount = (widget.videoModel?.collectCount ?? 1) - 1;
      }
      _isCollectLoading = false;
      widget.videoModel?.vidStatus?.hasCollected = !isCollect;
      if (!preCollect) {
        showToast("收藏成功");
      }
      setState(() {});
    } catch (e) {
      _isCollectLoading = false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 58,
      color: Color(0xff22242E),
      padding: EdgeInsets.symmetric(horizontal: 18.w),
      alignment: Alignment.centerLeft,
      child: Row(
        children: [
          Expanded(
            child: GestureDetector(
              onTap: () => widget.begainCommentHandler(false),
              child: Container(
                padding: EdgeInsets.symmetric(vertical: 2),
                child: GroupTextFiled(
                  controller: widget.textEditController,
                  height: 32,
                  enabled: false,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  textStyle: TextStyle(color: Colors.white, fontSize: 14),
                  placeholder: '留下真实评论的哥哥,都是最帅的男人',
                  placeholderTextStyle: TextStyle(color: Color(0x59FFFFFF), fontSize: 14),
                  decoration: BoxDecoration(color: Color(0x0DFFFFFF), borderRadius: BorderRadius.circular(40)),
                ),
              ),
            ),
          ),
          12.sizeBoxW,
          if (widget.isShowCollect)
            Container(
              width: 30,
              height: 30,
              alignment: Alignment.center,
              child: InkWell(
                onTap: _onCollectEvent,
                child: Image.asset(
                  isCollect ? "collect_red.png".commonImgPath : "collect_path.png".commonImgPath,
                  width: 24,
                ),
              ),
            ),
          Container(
            width: 30,
            height: 30,
            child: GestureDetector(
              onTap: () => widget.begainCommentHandler(true),
              child: widget.isSendStatus
                  ? CupertinoActivityIndicator(
                      color: AppColors.actionRed,
                      radius: 12,
                    )
                  : Image.asset('ic_send_red.webp'.commentPath),
            ),
          )
        ],
      ),
    );
  }
}
